/**
 * 
 */
package com.hwhp.service.console.impl;

import java.util.ArrayList;
import java.util.List;

import com.hwhp.common.Pager;
import com.hwhp.dao.BaseDao;
import com.hwhp.dao.HibernateUtil;
import com.hwhp.entity.Deptment;
import com.hwhp.service.console.DepartService;

/**
 * @author user
 *
 */
public class DepartServiceImpl implements DepartService {
	
    private BaseDao baseDao = null; 
    
	
   // private UserDao userDao = null ;
    
	public  List getFirstDepat(){
		String hql = " from  Deptment where parentId='-1'";
		List deptList = new ArrayList();
		//deptList = HibernateUtil.query(hql);
		deptList = baseDao.query(hql);
		return deptList ;
	}
	
	public   boolean deleteDepart(Deptment dept) throws Exception {
		return baseDao.delete(dept);
		//return HibernateUtil.delete(dept) ;
		
	}
    public  boolean insertDepart(Deptment dept){
    	boolean success = false ;
    	try{
    		//HibernateUtil.save(dept);
    		baseDao.save(dept);
    		success = true ;
    	}catch(Exception e){
    		success = false ;
    	}
    	return success ;
    }
   public  List getAllDept(){
	   List deptList = new ArrayList();
	   //deptList = HibernateUtil.query("from Deptment as d order by d.deptid asc");
	   deptList = baseDao.query("from Deptment as d order by d.deptid asc"); 
	   return deptList ;
   }
    public  boolean editDepart(Deptment dept){
    	boolean success = false ;
    	try{
    		
    		baseDao.update(dept);
    		//HibernateUtil.update(dept);
    		success = true ;
    	}catch(Exception e){
    		success = false ;
    	}
    	return success;
    	
    }
    public  List getChildDepts(String parentId){
    	System.out.println("deptId:"+parentId);
    	List deptList = new ArrayList();
    	String hql = "from Deptment as d where d.parentId='"+parentId+"'";
    	//deptList = baseDao.query(hql);
    	//deptList = departDao.getChildDepart(parentId);
    	//deptList = userDao.getChildDepart(parentId);
    	
    	deptList = HibernateUtil.query(hql);
    	
    	return deptList ;
    }
    
    public  Deptment getDepart(String deptId){
    	//Deptment dept = (Deptment) HibernateUtil.get(Deptment.class, deptId);
    	Deptment dept = (Deptment) baseDao.get(Deptment.class, deptId);
    	return dept ;
    }

    public Pager queryDepts(String deptName, String parentId ,int pageNumber) {
    	
		String hql = "from Deptment as d where d.parentId='"+parentId+"'and d.deptName like '%"+deptName+"%'";
		Pager deptPager = new Pager();
		try {
			deptPager = baseDao.queryByPage(hql,pageNumber,3);
			System.out.println(hql);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		return deptPager;
	}
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public BaseDao getBaseDao() {
		return baseDao;
	}
	


}
